﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Touch2Like.Model;
using System.Data;

namespace Touch2Like.DAL
{
    public class ActivityDAL
    {
        public int AddActivity(ActivityInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeOutParam("@ActivityID",SqlDbType.Int,4),
                db.MakeInParam("@TagUID",SqlDbType.VarChar,50,info.TagUID),
                db.MakeInParam("@TagContent",SqlDbType.VarChar,500,info.TagContent),
                db.MakeInParam("@TagType",SqlDbType.VarChar,50,info.TagType),
                db.MakeInParam("@TagDate",SqlDbType.DateTime,8,info.TagDate),
                db.MakeInParam("@DeviceId",SqlDbType.VarChar,50,info.DeviceID),
                db.MakeInParam("@IMSINumber",SqlDbType.VarChar,50,info.IMSINumber),
                db.MakeInParam("@PhoneNo",SqlDbType.Int,4,info.PhoneNo),
                db.MakeInParam("@PhoneModel",SqlDbType.VarChar,50,info.PhoneModel),
                db.MakeInParam("@Longitude",SqlDbType.VarChar,50,info.Longitude),
                db.MakeInParam("@Latitude",SqlDbType.VarChar,50,info.Latitude),
                db.MakeInParam("@BrowserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.BrowserGUID))
            };
            try
            {
                db.ExecNonQuery("CAM_Activities_Add", param);
                return DataHelper.GetInt(param[0].Value).Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        public int GetCountByItem(int Type, string Item)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@Type", SqlDbType.Int, 4, Type),
                db.MakeInParam("@Item", SqlDbType.VarChar, 50, Item)
            };
            try
            {
                dr = db.RunProc("CAM_Activities_GetCountByItem", param);
                if (dr.Read())
                {
                    return DataHelper.GetInt(dr[0]).Value;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }

        public int GetTotalCount()
        {
            Database db = new Database();
            SqlDataReader dr = null;
            try
            {
                dr = db.RunProc("CAM_Activities_GetTotalCount");
                if (dr.Read())
                {
                    return DataHelper.GetInt(dr[0]).Value;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }

        #region Get Day Statistics
        public DataTable GetDayStatistics(string customer, string taguid, DateTime begintime, DateTime endtime,string type)
        {
            Database db = new Database();

            try
            {
                if (string.IsNullOrEmpty(customer) || customer == "")
                {
                    SqlParameter[] param =
                    {
                        db.MakeInParam("@TagUID", SqlDbType.VarChar, 50, taguid),
                        db.MakeInParam("@BeginTime", SqlDbType.DateTime, 8, begintime),
                        db.MakeInParam("@EndTime", SqlDbType.DateTime, 8, endtime),
                        db.MakeInParam("@TagType",SqlDbType.VarChar,50, type)
                    };
                    return db.GetDataSet("CAM_Activities_GetDayStatistics", param).Tables[0];
                }
                else
                {
                    SqlParameter[] param1 =
                    {
                        db.MakeInParam("@UserGUID", SqlDbType.UniqueIdentifier, 16, new Guid(customer)),
                        db.MakeInParam("@TagUID", SqlDbType.VarChar, 50, taguid),
                        db.MakeInParam("@BeginTime", SqlDbType.DateTime, 8, begintime),
                        db.MakeInParam("@EndTime", SqlDbType.DateTime, 8, endtime),
                         db.MakeInParam("@TagType",SqlDbType.VarChar,50, type)
                    };
                    return db.GetDataSet("CAM_Activities_GetDayStatistics", param1).Tables[0];
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Get Day Statistics
        public DataTable GetDateStatistics(string UserGUID, string TagUID, DateTime BeginTime, DateTime EndTime,string type)
        {
            Database db = new Database();

            try
            {
                if (string.IsNullOrEmpty(UserGUID) || UserGUID == "")
                {
                    SqlParameter[] param =
                    {
                        db.MakeInParam("@TagUID", SqlDbType.VarChar, 50, TagUID),
                        db.MakeInParam("@BeginTime", SqlDbType.DateTime, 8, BeginTime),
                        db.MakeInParam("@EndTime", SqlDbType.DateTime, 8, EndTime),
                         db.MakeInParam("@TagType",SqlDbType.VarChar,50, type)
                    };
                    return db.GetDataSet("CAM_Activities_GetDateStatistics", param).Tables[0];
                }
                else
                {
                    SqlParameter[] param1 =
                    {
                        db.MakeInParam("@UserGUID", SqlDbType.UniqueIdentifier, 16, new Guid(UserGUID)),
                        db.MakeInParam("@TagUID", SqlDbType.VarChar, 50, TagUID),
                        db.MakeInParam("@BeginTime", SqlDbType.DateTime, 8, BeginTime),
                        db.MakeInParam("@EndTime", SqlDbType.DateTime, 8, EndTime),
                         db.MakeInParam("@TagType",SqlDbType.VarChar,50, type)
                    };
                    return db.GetDataSet("CAM_Activities_GetDateStatistics", param1).Tables[0];
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Get Day Tags Statistics
        public DataTable GetDayTagsStatistics(string customer, string taguid, DateTime begintime, DateTime endtime,string type)
        {
            Database db = new Database();

            try
            {
                if (string.IsNullOrEmpty(customer) || customer == "")
                {
                    SqlParameter[] param =
                    {
                        db.MakeInParam("@TagUID", SqlDbType.VarChar, 50, taguid),
                        db.MakeInParam("@BeginTime", SqlDbType.DateTime, 8, begintime),
                        db.MakeInParam("@EndTime", SqlDbType.DateTime, 8, endtime),
                         db.MakeInParam("@TagType",SqlDbType.VarChar,50, type)
                    };
                    return db.GetDataSet("CAM_Activities_GetDayTagsStatistics", param).Tables[0];
                }
                else
                {
                    SqlParameter[] param1 =
                    {
                        db.MakeInParam("@UserGUID", SqlDbType.UniqueIdentifier, 16, new Guid(customer)),
                        db.MakeInParam("@TagUID", SqlDbType.VarChar, 50, taguid),
                        db.MakeInParam("@BeginTime", SqlDbType.DateTime, 8, begintime),
                        db.MakeInParam("@EndTime", SqlDbType.DateTime, 8, endtime),
                         db.MakeInParam("@TagType",SqlDbType.VarChar,50, type)
                    };
                    return db.GetDataSet("CAM_Activities_GetDayTagsStatistics", param1).Tables[0];
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Get Day Tags Statistics
        public DataTable GetDateTagsStatistics(string UserGUID, string TagUID, DateTime BeginTime, DateTime EndTime,string type)
        {
            Database db = new Database();

            try
            {
                if (string.IsNullOrEmpty(UserGUID) || UserGUID == "")
                {
                    SqlParameter[] param =
                    {
                        db.MakeInParam("@TagUID", SqlDbType.VarChar, 50, TagUID),
                        db.MakeInParam("@BeginTime", SqlDbType.DateTime, 8, BeginTime),
                        db.MakeInParam("@EndTime", SqlDbType.DateTime, 8, EndTime),
                         db.MakeInParam("@TagType",SqlDbType.VarChar,50, type)
                    };
                    return db.GetDataSet("CAM_Activities_GetDateTagsStatistics", param).Tables[0];
                }
                else
                {
                    SqlParameter[] param1 =
                    {
                        db.MakeInParam("@UserGUID", SqlDbType.UniqueIdentifier, 16, new Guid(UserGUID)),
                        db.MakeInParam("@TagUID", SqlDbType.VarChar, 50, TagUID),
                        db.MakeInParam("@BeginTime", SqlDbType.DateTime, 8, BeginTime),
                        db.MakeInParam("@EndTime", SqlDbType.DateTime, 8, EndTime),
                         db.MakeInParam("@TagType",SqlDbType.VarChar,50, type)
                    };
                    return db.GetDataSet("CAM_Activities_GetDateTagsStatistics", param1).Tables[0];
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion
    }
}
